﻿using B.S.BasicData.API.Write.Application.Command.Role;
using B.S.BasicData.Domain.Entities.UserRoleMenu;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System.Transactions;

namespace B.S.BasicData.API.Write.Application.Handler.Role
{
    /// <summary>
    /// 删除角色信息请求命令处理类
    /// </summary>
    public class DelRoleHandler:IRequestHandler<DelRoleCommand,ApiResult<int>>
    {
        IBaseRepository<RoleModel> roleRep;
        IBaseRepository<UserRoleModel> userRoleRep;
        IBaseRepository<RoleMenuModel> roleMenuRep;
        public DelRoleHandler(IBaseRepository<RoleModel> roleRep, IBaseRepository<UserRoleModel> userRoleRep, IBaseRepository<RoleMenuModel> roleMenuRep)
        {
            this.roleRep = roleRep;
            this.userRoleRep = userRoleRep;
            this.roleMenuRep = roleMenuRep;
        }

        public Task<ApiResult<int>> Handle(DelRoleCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> res = new ApiResult<int>();

                var role = roleRep.GetAll().Where(x => x.Id == request.id).FirstOrDefault();
                if (role == null)
                {
                    res.Msg = "角色信息不存在";
                    res.Code = ApiEnum.Fail;
                    return Task.FromResult(res);
                }

                role.IsDeleted = true;
                role.UpdateDate = DateTime.Now;
                role.UpdateName = "李四";

                var userroles = userRoleRep.GetAll().Where(x => x.RoleId == role.Id).ToList();
                foreach (var item in userroles)
                {
                    item.IsDeleted = true;
                }

                var rolemenus = roleMenuRep.GetAll().Where(x => x.RoleId == role.Id).ToList();
                foreach (var item in rolemenus)
                {
                    item.IsDeleted = true;
                }

                int r2 = 0;
                int r1 = 0;
                int r = 0;

                using (TransactionScope tran = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    if (userroles.Count > 0)
                    {
                        r1 = userRoleRep.UpdateAsync(userroles).Result;
                    }
                    if (rolemenus.Count > 0)
                    {
                        r1 = roleMenuRep.UpdateAsync(rolemenus).Result;
                    }
                    r = roleRep.UpdateAsync(role).Result;
                    tran.Complete();
                }


                if (r > 0)
                {
                    res.Msg = "删除角色信息成功";
                    res.Data = r;
                    res.Code = ApiEnum.Success;
                }
                else
                {
                    res.Msg = "删除角色信息失败";
                    res.Code = ApiEnum.Fail;
                }

                return Task.FromResult(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
